package com.xywy.fwc;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.util.MD5Util;
import com.xywy.common.widget.zrcListView.SimpleFooter;
import com.xywy.common.widget.zrcListView.SimpleHeader;
import com.xywy.common.widget.zrcListView.ZrcListView;
import com.xywy.common.widget.zrcListView.ZrcListView.OnItemClickListener;
import com.xywy.common.widget.zrcListView.ZrcListView.OnStartListener;
import com.xywy.find.bean.FwcQuestionBean;
import com.xywy.fwc.adapter.FwcQuestionAdapter;
import com.xywy.fwc.net.DataCallBack;
import com.xywy.fwc.net.QuestionParser;
import com.xywy.fwc.net.Request;
import com.xywy.me.bean.UserKeeper;

public class MyQuestionActivity extends BaseActivity implements OnClickListener{
    private ZrcListView zrclist_view;
    FwcQuestionAdapter adapter;
    private List<FwcQuestionBean> list_bean = new ArrayList<FwcQuestionBean>();
 	private TextView tv_title_back;
 	private Button bt_title_back;
 	private int current_page = 1;
	private SharedPreferences sp;
	private String userid;
	
	private LinearLayout lin_process;
	private ProgressBar process;
	private TextView tv_reload;
    
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fwc_myquestion);
		initView();
		sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
//		userid = sp.getString("userid", "");
		userid = UserKeeper.getCurrentUser(this).getUserid();
//		userid = "15963569";
		adapter = new FwcQuestionAdapter(list_bean, this);
		zrclist_view.setAdapter(adapter);
		zrclist_view.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(ZrcListView parent, View view, int position, long id) {
				Intent intent = new Intent(MyQuestionActivity.this,FwcNewWebAct.class);
				intent.putExtra("url", list_bean.get(position).getUrl()+"&time="+(System.currentTimeMillis()/1000));
				intent.putExtra("title", "问题详情");
				startActivity(intent);
			}
		});
		lvOption();
		loadData();
	}
	@Override
	public void preInit() {
		
	}

	@Override
	public void loadData() {
		/**
		 * uid=15963569
		 * &pagenum=1
		 * &pagesize=10
		 * &sign=afca3e44aef3fce82730802eef23d774
		 */
		
		try {
			String url = FwcConstant.ASK_URL+
					"uid="+userid+
					"&pagenum="+current_page+
					"&pagesize=10"+"&sign="+MD5Util.md5(userid+current_page+"10"+FwcConstant.ASK_KEY);
			System.out.println("!!!!------"+url);
			Request.requestData(new QuestionParser(), call, url);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	DataCallBack<List<FwcQuestionBean>> call  =new DataCallBack<List<FwcQuestionBean>>() {
		
		@Override
		public void dataCall(List<FwcQuestionBean> list) {
			if(list != null){
					if(current_page ==1){
						zrclist_view.setRefreshSuccess("加载成功"); // 通知加载成功
						zrclist_view.startLoadMore(); // 开启LoadingMore功能
						list_bean.clear();
					}else {
						zrclist_view.setLoadMoreSuccess();
					}
					list_bean.addAll(list);
					System.out.println("------------00--"+list_bean.size());
					runOnUiThread(new Runnable() {
						public void run() {
							adapter.notifyDataSetChanged();
							if(list_bean != null && list_bean.size()>0){
							    zrclist_view.setLoadMoreSuccess();
								loadSuccess();
							}else {
								loadNoData();
							}
						}
					});
			
			}else{
				runOnUiThread(new Runnable() {
					public void run() {
						loadError();
					}
				});
			}
		}
	};

	@Override
	public void initView() {
		zrclist_view = (ZrcListView) findViewById(R.id.list_view);
		tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		bt_title_back = (Button) findViewById(R.id.bt_title_back);
		tv_title_back.setOnClickListener(this);
		bt_title_back.setOnClickListener(this);
		
		lin_process = (LinearLayout) findViewById(R.id.lin_progress);
    	process = (ProgressBar) findViewById(R.id.process);
    	tv_reload = (TextView) findViewById(R.id.tv_reload);
    	
    	tv_reload.setOnClickListener(this);
	}
	
	 public void loadError(){
	    	if(list_bean == null || list_bean.size()==0){
	    		zrclist_view.setVisibility(View.GONE);
	    		lin_process.setVisibility(View.VISIBLE);
	    		tv_reload.setText("加载失败");
	    		process.setVisibility(View.GONE);
	    	}
	    }
	    
	 public void loadSuccess(){
	    	zrclist_view.setVisibility(View.VISIBLE);
			lin_process.setVisibility(View.GONE);
	    }
	 public void loadNoData(){
		 zrclist_view.setVisibility(View.GONE);
		lin_process.setVisibility(View.VISIBLE);
		tv_reload.setText("没有数据");
		process.setVisibility(View.GONE);
	 }
	
	@Override
	public void onClick(View v) {
        switch (v.getId()) {
	    	case R.id.bt_title_back:
			case R.id.tv_title_back:
				finish();
				break;
			case R.id.tv_reload:
				if(tv_reload.getText().toString().equals("加载失败")){
					process.setVisibility(View.VISIBLE);
					tv_reload.setText("正在加载");
				}
				break;
		}		
	}
	
	private void lvOption() {
		// 设置默认偏移量，主要用于实现透明标题栏功能。（可选）
		float density = getResources().getDisplayMetrics().density;
		// lv.setFirstTopOffset((int) (0 * density));

		// 设置下拉刷新的样式（可选，但如果没有Header则无法下拉刷新）
		SimpleHeader header = new SimpleHeader(this);
		header.setTextColor(0xff666666);
		header.setCircleColor(0xff1fb5ad);
		zrclist_view.setHeadable(header);

		// 设置加载更多的样式（可选）
		SimpleFooter footer = new SimpleFooter(this);
		footer.setCircleColor(0xff1fb5ad);
		zrclist_view.setFootable(footer);

		// 设置列表项出现动画（可选）
		zrclist_view.setItemAnimForTopIn(R.anim.topitem_in);
		zrclist_view.setItemAnimForBottomIn(R.anim.bottomitem_in);

		// 下拉刷新事件回调（可选）
		zrclist_view.setOnRefreshStartListener(new OnStartListener() {
			@Override
			public void onStart() {
				current_page = 1;
				loadData();
//				refresh();
			}
		});

		// 加载更多事件回调（可选）
		zrclist_view.setOnLoadMoreStartListener(new OnStartListener() {
			@Override
			public void onStart() {
				current_page++;
//				loadMore();
				loadData();
			}
		});
//		lv.refresh(); // 主动下拉刷新

	}

}
